package com.jingdianjichi.auth.application.controller;

import com.jindianjichi.common.entity.Result;
import com.jingdianjichi.auth.application.convert.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthPermissionDTO;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;


    @PostMapping("add")
    public Result<Boolean> registry(@RequestBody @Validated AuthPermissionDTO authPermissionDTO) {
        try {
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean add = authPermissionDomainService.add(authPermissionBO);
            return Result.ok(add);
        } catch (Exception e) {
            log.error("注册失败:{}", e.getMessage());
            return Result.fail(false);
        }
    }

    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean update = authPermissionDomainService.update(authPermissionBO);
            return Result.ok(update);
        } catch (Exception e) {
            log.error("注册失败:{}", e.getMessage());
            return Result.fail(false);
        }
    }

    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);

            Boolean update = authPermissionDomainService.delete(authPermissionBO);
            return Result.ok(update);
        } catch (Exception e) {
            log.error("注册失败:{}", e.getMessage());
            return Result.fail(false);
        }
    }

    @PostMapping("getPermission")
    public Result<List<String>> getPermission(String userName){
        return Result.ok(authPermissionDomainService.getPermission(userName));
    }




}
